import { offlinePay, HelPayOrder } from "@/service/api/order.js"
import { getUserInfo } from "@/utils/appInit.js"
import store from "@/store/index.js"
import storage from "@/uni-sub-base/utils/storage.js"
import config from "@/config/config.js"
import { MiniProgramPay } from "@/service/api/open.js"
import { getBaseUrl } from "@/utils/appUrls.js"
const orderUtil = {
    //支付
    payForApi: (param) => {
        return new Promise((resolve, reject) => {
            offlinePay(param).then((res) => {
                if (res.IsOK) {
                    //更新用户余额
                    getUserInfo().then((res) => {})
                }
                resolve(res)
            })
        })
    },
    //微信支付
    payForWx: (data, type = 0) => {
        return new Promise((resolve, reject) => {
            let openId = storage.getCache("openId")
            //获取微信支付相关信息
            let notify_url = ""
            let pro = "订单"
            if (type == 1) {
                notify_url = getBaseUrl() + "Notify/PlusVipPay/" + data.BusinessId
                pro = "会员"
            }
            let payParam = {
                id: 0,
                pro: pro,
                order_sn: data.OutTradeNo,
                pay_sn: data.OutTradeNo,
                money: data.PayMoney,
                notify_url: notify_url,
                businessId: data.BusinessId,
                openId: openId,
            }
            MiniProgramPay(payParam).then((payRes) => {
                if (payRes.status) {
                    //微信支付api
                    uni.requestPayment({
                        provider: "wxpay",
                        timeStamp: payRes.data.timestamp,
                        nonceStr: payRes.data.nonceStr,
                        package: payRes.data.package,
                        signType: payRes.data.signType,
                        paySign: payRes.data.paySign,
                        success: function (res) {
                            uni.hideLoading()
                            resolve(true)
                        },
                        fail: function (err) {
                            uni.hideLoading()
                            resolve(false)
                        },
                    })
                }
            })
        })
    },
    //云支付
    helPayFn(obj, orderId, type = 0) {
        return new Promise((resolve, reject) => {
            let param = {
                orderId: orderId,
            }
            //type=1 购买 0 订单 2 合单支付
            let path = "?orderId=" + orderId + "&topupType=" + config.TOPUPTYPE + "&type=" + type
            HelPayOrder(param, path).then((res) => {
                uni.hideLoading()
                if (res.State == 1) {
                    let path = "pages/index/index?PaySn=" + res.Data.PaySn
                    obj.openMiniProgram(path, res.Data.MiniAppOriginal, res.Data.MiniAppId)
                    resolve(true)
                }
            })
        })
    },
    //富友支付
    payForFuiou: (data, type = 0) => {
        return new Promise((resolve, reject) => {
            let openId = storage.getCache("openId")
            //获取微信支付相关信息
            let notify_url = ""
            let pro = "订单"
            if (type == 1) {
                notify_url = getBaseUrl() + "Notify/PlusVipPay/" + data.BusinessId
                pro = "会员"
            }
            let payParam = {
                id: 0,
                pro: pro,
                order_sn: data.OutTradeNo,
                pay_sn: data.OutTradeNo,
                money: data.PayMoney,
                notify_url: notify_url,
                businessId: data.BusinessId,
                openId: openId,
            }
            FuiouMiniAppPay(payParam).then((payRes) => {
                if (payRes.status) {
                    //微信支付api
                    uni.requestPayment({
                        provider: "wxpay",
                        timeStamp: payRes.data.timestamp,
                        nonceStr: payRes.data.nonceStr,
                        package: payRes.data.package,
                        signType: payRes.data.signType,
                        paySign: payRes.data.paySign,
                        success: function (res) {
                            uni.hideLoading()
                            resolve(true)
                        },
                        fail: function (err) {
                            uni.hideLoading()
                            resolve(false)
                        },
                    })
                }
            })
        })
    },
}
module.exports = {
    payForApi: orderUtil.payForApi,
    payForWx: orderUtil.payForWx,
    helPayFn: orderUtil.helPayFn,
    payForFuiou: orderUtil.payForFuiou,
}
